package com.example.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfig;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import org.springframework.web.servlet.view.freemarker.FreeMarkerView;
import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver;

import java.util.Properties;

/**
 * @Project: SSM-JavaConfig
 * @Title: WebConfig
 * @Description: <p>定义 DispatcherServlet 加载应用上下文的配置</p>
 * @Author: zhuzhaofeng
 * @Email: zhuzhaofeng@aliyun.com
 * @dateTime: 2018-12-23 16:31
 * @Version: 1.0.0
 */
@Configuration
@EnableWebMvc
@ComponentScan("com.example.controller")
public class WebConfig implements WebMvcConfigurer {
    // jsp 视图解析器
    /*@Bean
    public ViewResolver viewResolver(){
        InternalResourceViewResolver resolver =
                new InternalResourceViewResolver();
        // 缓存
        resolver.setCache(false);
        // 视图前缀
        resolver.setPrefix("/WEB-INF/views/");
        // 视图后缀
        resolver.setSuffix(".jsp");
        return resolver;
    }*/

    //FreeMarker视图解析器
    @Bean
    public FreeMarkerViewResolver freeMarkerViewResolver() {
        FreeMarkerViewResolver markerViewResolver =
                new FreeMarkerViewResolver();
        markerViewResolver.setSuffix(".ftl");
        markerViewResolver.setOrder(1);
        markerViewResolver.setCache(false);
        markerViewResolver.setViewClass(FreeMarkerView.class);
        markerViewResolver.setContentType("text/html;charset=utf-8");
        return markerViewResolver;
    }

    @Bean
    public FreeMarkerConfigurer freeMarkerConfigurer() {
        FreeMarkerConfigurer freeMarkerConfigurer =
                new FreeMarkerConfigurer();
        freeMarkerConfigurer.setTemplateLoaderPath("/WEB-INF/views/");

        Properties settingProperties = new Properties();
        // 刷新模板的周期，单位为秒
        settingProperties.setProperty("template_update_delay", "5");
        settingProperties.setProperty("default_encoding", "utf-8");
        settingProperties.setProperty("datetime_format", "yyyy-MM-dd HH:mm:ss");
        settingProperties.setProperty("time_format", "HH:mm:ss");
        settingProperties.setProperty("url_escaping_charset", "utf-8");

        freeMarkerConfigurer.setFreemarkerSettings(settingProperties);

        return freeMarkerConfigurer;
    }

    //文件上传，bean必须写name属性且必须为multipartResolver，不然取不到文件对象
    @Bean(name = "multipartResolver")
    public CommonsMultipartResolver multipartResolver() {
        CommonsMultipartResolver multipartResolver =
                new CommonsMultipartResolver();
        multipartResolver.setMaxUploadSize(2097152); //2M
        //单个文件大小限制
        multipartResolver.setMaxUploadSizePerFile(0);
        multipartResolver.setDefaultEncoding("UTF-8");
        return multipartResolver;
    }

    //静态资源的处理
    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/home").setViewName("home");
        registry.addViewController("/index").setViewName("index");
        registry.addViewController("/").setViewName("index");
        registry.addViewController("/login").setViewName("login");
    }
}
